/*
 * Hérite de Transferable
 * Pour pouvoir transferer l'objet DnDData.
 */
package chipwits.dnd;

import chipwits.operator.Arrow;
import chipwits.operator.OpType;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import javax.swing.ImageIcon;

/**
 *
 * @author Emmanuel
 */
public class TransferableData implements Transferable {

    public static DataFlavor mData;
    private DnDData mValue;     //l'objet à transferer

    public TransferableData(OpType type, ImageIcon icone, Arrow[] arrows) {
        String mimeType = DataFlavor.javaJVMLocalObjectMimeType + ";class=" + DnDData.class.getName();
        try {
            mData = new DataFlavor(mimeType);
            mValue = new DnDData(type, arrows);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public DataFlavor[] getTransferDataFlavors() {
        return new DataFlavor[]{mData, DataFlavor.stringFlavor};
    }

    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return (flavor.equals(mData) || flavor.equals(DataFlavor.stringFlavor));
    }

    public Object getTransferData(DataFlavor df) throws UnsupportedFlavorException, IOException {
        if (df == null) {
            throw new IOException();
        }
        if (df.equals(mData)) {
            return mValue;
        }
        if (df.equals(DataFlavor.stringFlavor)) {   //si c'est un champ de texte, on donne un string
            return mValue.toString();
        } else {
            throw new UnsupportedFlavorException(df);
        }
    }
}
